<?php
/*
 * template/new.phtml
 */

use RHX\Helper\RHDateTime;
?>
<?php
// variables en provenance du controller
$site		 = $this->site;
$templates	 = $this->templates;
$colors		 = $this->colors;
$codec		 = $this->codec;
$codes		 = $this->codes;
// correctif : 0 => 3 Iconnue => OFF
$codec[0] = $codec[3];
// variables
$dayName	 = array(
    'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche',
);
//var_dump($colors);
//var_dump($codec);
//var_dump($codes);
?>
<div class="container-fluid">
    <div class="content-head">
	<form id="new-form" action="#" method="post" onsubmit="return false;">
	    <h3>
		Planning Type : <input type="text" name="libelle" value="Planning Type - <?php echo $site->libelle; ?>" class="input-xlarge">
	    </h3>
	    <div class="toolbar">
		<a class="btn btn-success btn-small" id="save-btn">Enregistrer</a>
		<a class="btn btn-inverse btn-small" id="cancel-btn">Annuler</a>
	    </div>
	    <input type="hidden" name="site" value="<?php echo $site->id; ?>">
	    <?php foreach ($templates as $tpl): ?>
    	    <input type="hidden" name="employes[<?php echo $tpl->matricule ?>]" value="<?php echo $tpl->matricule; ?>">
		<?php foreach ($dayName as $day): ?>
		    <?php
		    $dayCode	 = $day . '_code';
		    $dayEntree	 = $day . '_entree';
		    $daySortie	 = $day . '_sortie';
		    $dayPause	 = $day . '_pause';
		    $dayRepas	 = $day . '_repas';
		    ?>
		    <input type="hidden" id="new-<?php echo $tpl->matricule . '-' . $day . '-code' ?>" name="<?php echo $tpl->matricule . '-' . $day . '-code' ?>" value="<?php echo $tpl->$dayCode; ?>">
		    <input type="hidden" id="new-<?php echo $tpl->matricule . '-' . $day . '-entree' ?>" name="<?php echo $tpl->matricule . '-' . $day . '-entree' ?>" value="<?php echo RHDateTime::hourNoSecond($tpl->$dayEntree); ?>">
		    <input type="hidden" id="new-<?php echo $tpl->matricule . '-' . $day . '-sortie' ?>" name="<?php echo $tpl->matricule . '-' . $day . '-sortie' ?>" value="<?php echo RHDateTime::hourNoSecond($tpl->$daySortie); ?>">
		    <input type="hidden" id="new-<?php echo $tpl->matricule . '-' . $day . '-pause' ?>" name="<?php echo $tpl->matricule . '-' . $day . '-pause' ?>" value="<?php echo $tpl->$dayPause; ?>">
		    <input type="hidden" id="new-<?php echo $tpl->matricule . '-' . $day . '-repas' ?>" name="<?php echo $tpl->matricule . '-' . $day . '-repas' ?>" value="<?php echo $tpl->$dayRepas; ?>">
		<?php endforeach; ?>
    	    <input type="hidden" id="new-heure-prevues" name="heure_prevues" value="<?php echo $tpl->heure_prevues; ?>">
	    <?php endforeach; ?>
	</form>
    </div>

    <div class="content-body">
	<table class="table table-condensed list-table black-link" style="font-size: 0.9em;">
	    <thead>
		<tr>
		    <td rowspan='2'>Mat.</td>
		    <td rowspan='2'>Poste</td>
		    <td rowspan='2'>Prénom</td>
		    <td rowspan='2'>Horaire Hebdo.</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Lundi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Mardi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Mercredi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Jeudi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Vendredi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Samedi</td>
		    <td colspan='5' style="border-right: solid 2px #DDDDDD">Dimanche</td>
		    <td rowspan="2">Cumul des heures</td>
		</tr>
		<tr>
		    <?php foreach ($dayName as $day): ?>
    		    <td>Code</td>
    		    <td>Entrée</td>
    		    <td>Sortie</td>
    		    <td>P</td>
    		    <td style="border-right: solid 2px #DDDDDD">R</td>
		    <?php endforeach; ?>
		</tr>
	    </thead>
	    <tbody>
		<!-- CONTENU PRINCIPALE -->
		<?php $jaune = 1; ?>
		<?php foreach ($templates as $tpl): ?>
		    <?php
		    $employe = $tpl->getEmploye();
		    $poste	 = $employe->getPoste();
		    $hebdo	 = $employe->getHoraireHebdo(true);
		    ?>
    		<tr>
    		    <td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><?php echo $employe->getMatricule(true); ?></td>
    		    <td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><?php echo $poste->code; ?></td>
    		    <td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><a href="<?php echo $this->url('RHX/default', array('controller' => 'employe', 'action' => 'edit', 'id' => $employe->matricule)) ?>" target="_blank"><?php echo $employe->prenom; ?></a></td>
    		    <td<?php echo (($jaune % 2) == 0) ? ' class="jaune2"' : ' class="jaune1"'; ?>><?php echo $hebdo; ?></td>
			<?php foreach ($dayName as $day): ?>
			    <?php
			    $dayCode	 = $day . '_code';
			    $dayEntree	 = $day . '_entree';
			    $daySortie	 = $day . '_sortie';
			    $dayPause	 = $day . '_pause';
			    $dayRepas	 = $day . '_repas';
			    ?>
			    <td class="<?php echo $codec[(int) $tpl->$dayCode]; ?>">
				<a href='#' class='template-edit' data-id='0' data-matricule='<?php echo $employe->matricule; ?>' data-prefix="<?php echo $day ?>" data-toggle="popover" id="<?php echo $employe->matricule .'-' .$day .'-code'; ?>"><?php echo $codec[(int) $tpl->$dayCode]; ?></a>
			    </td>
			    <td class="<?php echo $codec[(int) $tpl->$dayCode]; ?>">
				<a href='#' class='template-edit' data-id='0' data-matricule='<?php echo $employe->matricule; ?>' data-prefix="<?php echo $day ?>" data-toggle="popover" id="<?php echo $employe->matricule .'-' .$day .'-entree'; ?>"><?php echo RHDateTime::hourNoSecond($tpl->$dayEntree); ?></a>
			    </td>
			    <td class="<?php echo $codec[(int) $tpl->$dayCode]; ?>">
				<a href='#' class='template-edit' data-id='0' data-matricule='<?php echo $employe->matricule; ?>' data-prefix="<?php echo $day ?>" data-toggle="popover" id="<?php echo $employe->matricule .'-' .$day .'-sortie'; ?>"><?php echo RHDateTime::hourNoSecond($tpl->$daySortie); ?></a>
			    </td>
			    <td class="<?php echo $codec[(int) $tpl->$dayCode]; ?>">
				<a href='#' class='template-edit' data-id='0' data-matricule='<?php echo $employe->matricule; ?>' data-prefix="<?php echo $day ?>" data-toggle="popover" id="<?php echo $employe->matricule .'-' .$day .'-pause'; ?>"><?php echo ((int) $tpl->$dayPause == 1) ? 'X' : '' ?></a>
			    </td>
			    <td class="<?php echo $codec[(int) $tpl->$dayCode]; ?>" style="border-right: solid 2px #DDDDDD">
				<a href='#' class='template-edit' data-id='0' data-matricule='<?php echo $employe->matricule; ?>' data-prefix="<?php echo $day ?>" data-toggle="popover" id="<?php echo $employe->matricule .'-' .$day .'-repas'; ?>"><?php echo ((int) $tpl->$dayRepas == 1) ? '1' : '' ?></a>
			    </td>
			<?php endforeach; ?>
		    <?php 
		    $val = RHDateTime::toFloat($tpl->heure_prevues);
		    $hebdo = $employe->getHoraireHebdo();
		    $class = '';
		    if($val == $hebdo){
			$class = 'value-equal';
		    }elseif($val > $hebdo){
			$class = 'value-greater';
		    }else{
			$class = 'value-less';
		    }
		    ?>
    		    <td class="<?php echo $class; ?>"><?php //echo RHDateTime::hourNoSecond($tpl->heure_prevues); ?></td>
    		</tr>
		<?php $jaune++; ?>
		<?php endforeach; ?>
	    </tbody>
	</table>
    </div>
</div>

<script type="text/javascript">
    $(function() {
	// annuler
	$('#cancel-btn').click(function(e) {
	    e.preventDefault();
	    normalScreenLayout();
	});
	// sauvegarder
	$('#save-btn').click(function(e) {
	    e.preventDefault();
	    $(this).html("<img src='<?php echo $this->basePath() .'/img/rhx/ajax-waiting.gif' ?>'>");
	    ajax = $.ajax({
		type: 'post',
		url : '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'saveNew')) ?>',
		data: $('#new-form').serialize()
	    });
	    ajax.done(function(result){
		$('#cancel-btn').click();
		refresh();
	    });
	});
	// editer
	$('.template-edit').click(function(){
	    $('.popover').remove();
	});
	$('.template-edit').popover({
	    html: true,
	    title: '<center>Edition du Planning Type</center>',
	    placement: 'bottom',
	    content: function(){
		id = $(this).attr('data-id');
		matricule = $(this).attr('data-matricule');
		prefix = $(this).attr('data-prefix');
		selector = '#new-' + matricule + '-' + prefix;
		entree = $(selector + '-entree').val();
		sortie = $(selector + '-sortie').val();
		code = $(selector + '-code').val();
		pause = $(selector+  '-pause').val();
		repas = $(selector + '-repas').val();
		ajax = $.ajax({
		    type: 'post',
		    url : '<?php echo $this->url('RHX/default', array('controller' => 'template', 'action' => 'popover')) ?>',
		    data: '&matricule=' + matricule + '&entree=' + entree + '&sortie=' + sortie + '&code=' + code + '&pause=' + pause + '&repas=' + repas + '&prefix=' + prefix + '&id=' + id
		});
		ajax.done(function(result){
		    $('.popover-content').html(result);
		});
	    }
	});
    });
</script>

<style type="text/css">
    <?php foreach ($colors as $c => $couleur): ?>
    .<?php echo $c ?>{
	background: <?php echo $couleur; ?>
    }
    <?php endforeach; ?>
    .jaune1{
	background: #FFF67F;
    }
    .jaune2{
	background: #FFEE05;
    }
    .black-link a{
	color: #000;
    }
    .popover{
	width: 550px;
    }
    .value-equal {
	background: #CCCCFF;
    }
    .value-greater {
	background: #FF6633;
	color: #FFFFFF;
    }
    .value-less {
	background: #CC0000;
	color: #FFFFFF;
    }
</style>